package com.ruoyi.web.controller.sfproduct;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.sfproduct.domain.vo.MaterialVo;
import org.aspectj.weaver.patterns.IToken;
import org.aspectj.weaver.patterns.ITokenSource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sfproduct.domain.LwSfproduct;
import com.ruoyi.sfproduct.service.ILwSfproductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 半成品Controller
 * 
 * @author sll
 * @date 2023-08-09
 */

@RestController
@RequestMapping("/sfproduct/sfproduct")
public class LwSfproductController extends BaseController
{
    @Autowired
    private ILwSfproductService lwSfproductService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询半成品列表
     */
    @PreAuthorize("@ss.hasPermi('sfproduct:sfproduct:list')")
    @GetMapping("/list")
    public TableDataInfo list(LwSfproduct lwSfproduct)
    {
        startPage();
        List<LwSfproduct> list = lwSfproductService.selectLwSfproductList(lwSfproduct);
        return getDataTable(list);
    }

    /**
     * 获取半成品元件清单列表
     */
    @GetMapping("/getMaterialVo/{sfpId}")
    public TableDataInfo getMaterialVoInfo(@PathVariable("sfpId") Long sfpId)
    {
        List<MaterialVo> list = lwSfproductService.selectMaterialVoBySfproductId(sfpId);
        return getDataTable(list);
    }

    /**
     * 获取半成品原材料中间表信息
     */
    @GetMapping("/getMiddle/")
    public MaterialVo getMiddle(@RequestParam(required = false) Long sfproductId,
                                @RequestParam(required = false) Long materialId)
    {
        return lwSfproductService.selectMaterialAndSfproduct(sfproductId,materialId);
    }

    /**
     * 导出半成品列表
     */
    @PreAuthorize("@ss.hasPermi('sfproduct:sfproduct:export')")
    @Log(title = "半成品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LwSfproduct lwSfproduct)
    {
        List<LwSfproduct> list = lwSfproductService.selectLwSfproductList(lwSfproduct);
        ExcelUtil<LwSfproduct> util = new ExcelUtil<LwSfproduct>(LwSfproduct.class);
        util.exportExcel(response, list, "半成品数据");
    }

    /**
     * 获取半成品详细信息
     */
    @PreAuthorize("@ss.hasPermi('sfproduct:sfproduct:query')")
    @GetMapping(value = "/{sfproductId}")
    public AjaxResult getInfo(@PathVariable("sfproductId") Long sfproductId)
    {
        return success(lwSfproductService.selectLwSfproductBySfproductId(sfproductId));
    }

    /**
     * 新增半成品
     */
    @PreAuthorize("@ss.hasPermi('sfproduct:sfproduct:add')")
    @Log(title = "半成品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LwSfproduct lwSfproduct)
    {
        return toAjax(lwSfproductService.insertLwSfproduct(lwSfproduct));
    }

    /**
     * 新增原材料
     */
    @Log(title = "原材料", businessType = BusinessType.INSERT)
    @PostMapping("/addMaterials")
    public AjaxResult addMaterials(@RequestBody MaterialVo materialVo)
    {
        return toAjax(lwSfproductService.addMaterials(materialVo));
    }

    /**
     * 修改半成品
     */
    @PreAuthorize("@ss.hasPermi('sfproduct:sfproduct:edit')")
    @Log(title = "半成品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LwSfproduct lwSfproduct)
    {
        return toAjax(lwSfproductService.updateLwSfproduct(lwSfproduct));
    }

    /**
     * 修改半成品
     */
    @Log(title = "原材料", businessType = BusinessType.UPDATE)
    @PutMapping("/updateMaterials")
    public AjaxResult updateMaterials(@RequestBody MaterialVo materialVo)
    {
        return toAjax(lwSfproductService.updateMaterials(materialVo));
    }

     /**
     * 删除半成品
     */
    @PreAuthorize("@ss.hasPermi('sfproduct:sfproduct:remove')")
    @Log(title = "半成品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sfproductIds}")
    public AjaxResult remove(@PathVariable Long[] sfproductIds)
    {
        return toAjax(lwSfproductService.deleteLwSfproductBySfproductIds(sfproductIds));
    }

    @DeleteMapping("/delMaterial/{sfproductId}/{materialId}")
    public AjaxResult delMaterial(@PathVariable("sfproductId") Long sfproductId,
                                @PathVariable("materialId") Long materialId)
    {
        return toAjax(lwSfproductService.delMaterial(sfproductId,materialId));
    }

    @DeleteMapping("/overAgain/{sfproductId}")
    public AjaxResult overAgain(@PathVariable("sfproductId") Long sfproductId)
    {
        return toAjax(lwSfproductService.overAgain(sfproductId));
    }

    /**
     * 导入数据管理列表
     */
    @Log(title = "数据管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<MaterialVo> util = new ExcelUtil<MaterialVo>(MaterialVo.class);
        List<MaterialVo> dataMaterialVoList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = lwSfproductService.importData(dataMaterialVoList, updateSupport, operName);
        return AjaxResult.success(message);
    }
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<MaterialVo> util = new ExcelUtil<MaterialVo>(MaterialVo.class);
        return util.importTemplateExcel("贴片数据");
    }

    @PutMapping("/transmissionSfpID")
    public void transmissionSfpID(@RequestParam Long sfproductId)
    {
        lwSfproductService.transmissionSfpID(sfproductId);
    }
}
